package com.ex.ui.questionpanel;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.SystemColor; 
import com.ex.shapejudge.TriangleJudgeQuestion;
import com.ex.ui.infoframe.PersonInfoIFrame;

import javax.swing.JTextField;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class TriangleJudgePanel extends JPanel {

	 private  JRadioButton mYesBtn;
	 private JRadioButton mNoBtn;
	 private JButton mCommitBtn;
	 private JLabel mRightAnswerTa;
	 private JTextArea mQustionTa;
	 private JTextField mALengthTf;
	 private JTextField mBLengthTf;
	 private JTextField mCLengthTf;
	 private TriangleJudgeQuestion mJudge;
	 private JCheckBox mAuto;
	 private PersonInfoIFrame mScoreFrame;

	/**
	 * Create the application.
	 */
	public TriangleJudgePanel(PersonInfoIFrame f) {
		mScoreFrame = f;
		setToolTipText("三角形判断");
		initialize();
		mJudge = new TriangleJudgeQuestion();
		fillAbc();
		mRightAnswerTa.setVisible(false);
		setName();
	}

	private void fillAbc() {
		this.mALengthTf.setText(String.valueOf(mJudge.getBorderA()));
		this.mBLengthTf.setText(String.valueOf(mJudge.getBorderB()));
		this.mCLengthTf.setText(String.valueOf(mJudge.getBorderC()));

	}
	

	public void setScore(boolean right)
	{
		if (mScoreFrame!=null)
		{ 
			mScoreFrame.addQustion(right); 
		}
	} 

	private void setAbcEnable(boolean e) {
		this.mALengthTf.setEnabled(e);
		this.mBLengthTf.setEnabled(e);
		this.mCLengthTf.setEnabled(e);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		this.setBounds(100, 100, 452, 281);
		this.setLayout(null);

		JLabel label = new JLabel("题目：");
		label.setBounds(26, 10, 45, 18);
		this.add(label);

		JLabel lebel1 = new JLabel("答案：");
		lebel1.setBounds(26, 159, 45, 18);
		this.add(lebel1);

		ButtonGroup group = new ButtonGroup();

		mYesBtn = new JRadioButton("是");
		mYesBtn.setSelected(true);
		mYesBtn.setBounds(81, 155, 43, 27);
		this.add(mYesBtn);

		mNoBtn = new JRadioButton("否");
		mNoBtn.setBounds(142, 155, 43, 27);
		this.add(mNoBtn);

		group.add(mYesBtn);
		group.add(mNoBtn);

		mCommitBtn = new JButton("提交");
		mCommitBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean resulte = false;
				if (mYesBtn.isSelected()) {
					resulte = true;
				}
				if (!mAuto.isSelected()) {
					int a = Integer.valueOf(mALengthTf.getText());
					int b = Integer.valueOf(mALengthTf.getText());
					int c = Integer.valueOf(mALengthTf.getText());
					mJudge.createQuestion(a, b, c);
				}

				boolean iright = mJudge.setJudge(resulte);

			  	if (iright)
				{
					mRightAnswerTa.setText("回答正确！");
					mRightAnswerTa.setForeground(Color.GREEN);
				}
				else
				{
					mRightAnswerTa.setText("回答错误！");
					mRightAnswerTa.setForeground(Color.red);
				}
			  	setScore(iright);
				mRightAnswerTa.setVisible(true);
			}
		});
		mCommitBtn.setBounds(235, 155, 63, 27);
		this.add(mCommitBtn);

		mRightAnswerTa = new JLabel();
		mRightAnswerTa.setBounds(26, 190, 396, 27); 
		mRightAnswerTa.setText("正确答案：");
		mRightAnswerTa.setForeground(Color.RED);
		mRightAnswerTa.setFont(new Font("Monospaced", Font.BOLD, 17));
		this.add(mRightAnswerTa);

		mQustionTa = new JTextArea();
		mQustionTa.setBounds(69, 7, 353, 23);
		mQustionTa.setForeground(Color.BLUE);
		mQustionTa.setFont(new Font("宋体", Font.BOLD, 16));
		mQustionTa.setBackground(SystemColor.inactiveCaptionBorder);
		mQustionTa.setText("三条边，边长如下，请问能否构成一个三角形。");
		mQustionTa.setWrapStyleWord(true);
		this.add(mQustionTa);
		mQustionTa.setOpaque(false);
		mRightAnswerTa.setOpaque(false);

		mALengthTf = new JTextField();
		mALengthTf.setBounds(169, 43, 35, 24);
		add(mALengthTf);
		mALengthTf.setColumns(10);

		JTextArea txtrA = new JTextArea();
		txtrA.setWrapStyleWord(true);
		txtrA.setText("A边长度为：");
		txtrA.setOpaque(false);
		txtrA.setForeground(Color.BLUE);
		txtrA.setFont(new Font("宋体", Font.BOLD, 16));
		txtrA.setBackground(SystemColor.inactiveCaptionBorder);
		txtrA.setBounds(69, 43, 93, 23);
		add(txtrA);

		JTextArea txtrB = new JTextArea();
		txtrB.setWrapStyleWord(true);
		txtrB.setText("B边长度为：");
		txtrB.setOpaque(false);
		txtrB.setForeground(Color.BLUE);
		txtrB.setFont(new Font("宋体", Font.BOLD, 16));
		txtrB.setBackground(SystemColor.inactiveCaptionBorder);
		txtrB.setBounds(69, 76, 98, 23);
		add(txtrB);

		JTextArea txtrC = new JTextArea();
		txtrC.setWrapStyleWord(true);
		txtrC.setText("C边长度为：");
		txtrC.setOpaque(false);
		txtrC.setForeground(Color.BLUE);
		txtrC.setFont(new Font("宋体", Font.BOLD, 16));
		txtrC.setBackground(SystemColor.inactiveCaptionBorder);
		txtrC.setBounds(69, 112, 98, 23);
		add(txtrC);

		mBLengthTf = new JTextField();
		mBLengthTf.setColumns(10);
		mBLengthTf.setBounds(169, 79, 35, 24);
		add(mBLengthTf);

		mCLengthTf = new JTextField();
		mCLengthTf.setColumns(10);
		mCLengthTf.setBounds(169, 112, 35, 24);
		add(mCLengthTf);

		mAuto = new JCheckBox("自动出题");
		mAuto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (mAuto.isSelected()) {
					mJudge.createQuestion();
					fillAbc();
					setAbcEnable(false);
					mRightAnswerTa.setVisible(false);
				} else {
					setAbcEnable(true);
				}
			}
		});
		mAuto.setSelected(true);
		mAuto.setBounds(289, 115, 133, 27);
		add(mAuto);
		
		JButton mNextBtn = new JButton("出题");
		mNextBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mJudge.createQuestion();
				fillAbc();
			}
		});
		mNextBtn.setName("mNextBtn");
		mNextBtn.setBounds(310, 155, 63, 27);
		add(mNextBtn);
		
		
	}

	private void setName()
	{
		 mYesBtn.setName("mYesBtn"); 
		 mNoBtn.setName("mNoBtn");
		 mCommitBtn.setName("mCommitBtn"); 
		 mRightAnswerTa.setName("mRightAnswerTa");  
		 mALengthTf.setName("mALengthTf"); 
		 mBLengthTf.setName("mBLengthTf"); 
		 mCLengthTf.setName("mCLengthTf"); 
		 mAuto.setName("mAuto");
	}
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TriangleJudgePanel p = new TriangleJudgePanel(null);
					JFrame window = new JFrame();
					window.setContentPane(p);
					window.setSize(p.getWidth(), p.getHeight());
					window.setLocationRelativeTo(null);
					window.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
